import unittest, requests
from rikao_01.mydb import Datashuju


class A3mall(unittest.TestCase):

    def setUp(self) -> None:
        print('start case')
        self.db = Datashuju('10.59.9.14', 'root', 'yidong666', 'a3mall')

    def tearDown(self) -> None:
        print('end case')
        self.db.close()

    # 登录
    mytoken = None
    group_name = None
    nickname = None
    username = None
    mobile = None

    def test_01(self):
        r1 = requests.post(url='http://10.59.9.14:7003/api/public/login',
                           json={"username": "15835440857", "password": "123456"})
        print(r1.json())
        A3mall.mytoken = {'auth-token': 'Bearer ' + r1.json()['data']['token']}
        A3mall.group_name = r1.json()['data']['group_name']
        A3mall.nickname = r1.json()['data']['nickname']
        A3mall.username = r1.json()['data']['username']
        A3mall.mobile = r1.json()['data']['mobile']
        # 包含断言
        self.assertIn('会员', A3mall.group_name)
        # 相等断言
        self.assertEqual('15835440857', A3mall.nickname)
        # 正确断言
        self.assertTrue('15835440857' == A3mall.username)
        # 相等断言
        self.assertEqual('15835440857', A3mall.mobile)

        # 数据库断言
        group_name_dy = self.db.select(f"select mobile from mall_users where mobile={A3mall.mobile}")

        print(group_name_dy[0][0])
        self.assertIn(A3mall.mobile, group_name_dy[0][0])

    # 查询
    def test_02(self):
        r2 = requests.get(url='http://10.59.9.14:7003/api/search',
                          headers=A3mall.mytoken)
        print(r2.json())

    # 商品列表
    spid = None
    title = None

    def test_03(self):
        r3 = requests.get(url='http://10.59.9.14:7003/api/search/list?page=1&keywords=%E6%B5%8B%E8%AF%95&type=0&sort=1',
                          headers=A3mall.mytoken)
        print(r3.json())
        A3mall.spid = r3.json()['data']['list'][0]['id']
        A3mall.title = r3.json()['data']['list'][0]['title']
        # 不相等断言
        self.assertNotEqual('呵呵', A3mall.title)

    # 商品详情
    def test_04(self):
        r4 = requests.get(url='http://10.59.9.14:7003/api/goods/view?id=' + str(A3mall.spid),
                          headers=A3mall.mytoken)
        print(r4.json())

    # 添加购物车
    def test_05(self):
        r5 = requests.post(url='http://10.59.9.14:7003/api/cart/add',
                           headers=A3mall.mytoken,
                           json={"id": A3mall.spid, "sku_id": "", "num": 1})
        print(r5.json())

    gwcid = None

    def test_06(self):
        r6 = requests.get(url='http://10.59.9.14:7003/api/cart?page=1',
                          headers=A3mall.mytoken)
        print(r6.json())
        A3mall.gwcid = r6.json()['data']['list'][0]['id']

    # 提交订单
    def test_07(self):
        r7 = requests.get(
            url='http://10.59.9.14:7003/api/order/confirm?id=' + str(A3mall.gwcid) + '&type=cart&shipping_type=1',
            headers=A3mall.mytoken)
        print(r7.json())

    # 创建订单
    def test_08(self):
        r8 = requests.post(url='http://10.59.9.14:7003/api/order/create',
                           headers=A3mall.mytoken,
                           json={"id": str(A3mall.gwcid), "type": "cart", "address_id": 47, "store_id": 1,
                                 "shipping_type": 1,
                                 "bonus_id": "0", "payment": "balance", "remarks": "", "source": 1,
                                 "url": "http://10.59.9.14:7003/wap/pages/cart/confirm?id=657&type=cart"})
        print(r8.json())


if __name__ == '__main__':
    # 第一种
    unittest.main()
    # 第二种
    suite = unittest.defaultTestLoader.discover(start_dir='./rk', pattern='mqk0422*.py')
    unittest.TextTestRunner.run(suite)
